package cn.ffcs.bpm.service;


import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import cn.ffcs.base.service.BaseService;
import cn.ffcs.bpm.dao.DocumentTypeDAO;
import cn.ffcs.bpm.dao.NextTepDAO;
import cn.ffcs.bpm.model.DocumentType;
import cn.ffcs.bpm.model.NextTep;
import cn.ffcs.bpm.model.ProcessConfigDetail;
import cn.ffcs.system.dict.model.Dict;

/**
 * 流程节点配置服务层<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class ProcessConfigDetailService<D extends ProcessConfigDetail> extends BaseService<D>
implements IProcessConfigDetailService<D>
{

    /** 办理人类型服务 **/
    private NextTepDAO nextTepDAO;
    /** 文档类型服务 **/
    private DocumentTypeDAO documentTypeDAO;
    
    /**
     * 删除流程实例
     * @param b
     * @return
     */
    public void remove(D processConfigDetail)
    {
        super.remove(processConfigDetail);
        nextTepDAO.remove(null, "processDetail", processConfigDetail.getId()+"");
    }

    
    public void saveOrUpdate(D processConfigDetail){
        super.saveOrUpdate(processConfigDetail);
        nextTepDAO.remove(null, "processDetail", processConfigDetail.getId()+"");
        ActionContext ctx = ActionContext.getContext();        
        HttpServletRequest request = (HttpServletRequest)ctx.get(ServletActionContext.HTTP_REQUEST);
        String newConfig=request.getParameter("newConfigIds"); 
        String docType=request.getParameter("docTypeIds"); 
        if(StringUtils.isNotEmpty(newConfig)){
            String [] newConfigIds =newConfig.split("#");
            if(newConfigIds!=null&&newConfigIds.length>0){
                for(String newConfigId:newConfigIds){
                    NextTep nextTep=new NextTep();
                    nextTep.setProcessDetail(processConfigDetail);
                    nextTep.setNextConfig(super.findById(new Integer(newConfigId)));
                    nextTepDAO.save(nextTep);
                }
            }
        }
        documentTypeDAO.remove(null, "processDetail", processConfigDetail.getId()+"");
        if(StringUtils.isNotEmpty(docType)){
            String [] docTypeIds =docType.split("#");
            if(docTypeIds!=null&&docTypeIds.length>0){
                for(String docTypeId:docTypeIds){
                    DocumentType documentType=new DocumentType();
                    documentType.setProcessDetail(processConfigDetail);
                    documentType.setDocType(new Dict(new Integer(docTypeId)));
                    documentTypeDAO.save(documentType);
                }
            }
        }
        return;
    }
    
    public void saveOrUpdate(D processConfigDetail,String newConfig){
        super.saveOrUpdate(processConfigDetail);
        nextTepDAO.remove(null, "processDetail", processConfigDetail.getId()+"");
        if(StringUtils.isNotEmpty(newConfig)){
            String [] newConfigIds =newConfig.split("#");
            if(newConfigIds!=null&&newConfigIds.length>0){
                for(String newConfigId:newConfigIds){
                    NextTep nextTep=new NextTep();
                    nextTep.setProcessDetail(processConfigDetail);
                    nextTep.setNextConfig(super.findById(new Integer(newConfigId)));
                    nextTepDAO.save(nextTep);
                }
            }
        }
        
        return;
    }
    public NextTepDAO getNextTepDAO()
    {
        return nextTepDAO;
    }

    public void setNextTepDAO(NextTepDAO nextTepDAO)
    {
        this.nextTepDAO = nextTepDAO;
    }


    public DocumentTypeDAO getDocumentTypeDAO()
    {
        return documentTypeDAO;
    }


    public void setDocumentTypeDAO(DocumentTypeDAO documentTypeDAO)
    {
        this.documentTypeDAO = documentTypeDAO;
    }
    
}
